package com.cm.log_process.log_common.entity;

import com.cm.log_process.log_common.exp.ParamNotMatchException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * Creator:郭兆鹏
 * Date:2019/12/19
 * 思路：请求参数实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestParam {

    private static Properties prop;

    static{
        try {
            prop = PropertiesLoaderUtils.loadAllProperties("application.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static Long INTEVALTIME = Long.parseLong(prop.getProperty("INTEVALTIME"));

    private String indexName;
    private String hostIp;
    private String transId;
    private String fileType;
    private String phoneNum;
    private Long startTime;
    private Long endTime;
    private Integer pageIndex = 1;
    private Integer pageSize = 5;

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getPhoneNum() {
        if(StringUtils.isNotEmpty(phoneNum)){
            if(phoneNum.length()<11||phoneNum.length()>15){
                throw new ParamNotMatchException("请输入正确的手机号!");
            }
            //这里排除掉前缀，除此二前缀之外的都会在后续的正则匹配中抛出异常
            if(phoneNum.startsWith("+86")){
                return phoneNum.substring(3,phoneNum.length());
            }else if(phoneNum.startsWith("86")){
                return phoneNum.substring(2,phoneNum.length());
            }else{
                return phoneNum;
            }
        }
        return "";
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }


    //判断是否为空，并设置默认值
    public Long getEndTime() {
        if(endTime==null && startTime!=null){
            this.endTime = startTime + INTEVALTIME;
        }
        if(endTime==null && startTime==null){
            this.endTime = System.currentTimeMillis();
        }
        return endTime;
    }

    //判断是否为空，并设置默认值
    public Long getStartTime() {
        if(startTime == null && endTime != null){
            System.out.println("INTEVALTIME = " + INTEVALTIME);
            this.startTime = endTime - this.INTEVALTIME;
        }
        if(startTime == null && endTime == null){
            this.startTime = System.currentTimeMillis() - INTEVALTIME;
        }
        return startTime;
    }


    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        if(pageIndex == null||pageIndex==0){
            return ;
        }
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if(pageSize == null||pageSize == 0){
            return ;
        }
        this.pageSize = pageSize;
    }

}
